﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

// Suburban or urban housing; Library; Museum; Primary School; Park; Grocery Store
public class ResidentialDistrict : District
{


    public ResidentialDistrict(City city)
        : base(DistrictType.Residential, city)
    {
    }


    // Scoring: It's either an urban residential or a suburban residential area.
    // It's whichever we have more of.
    // If it's a tie, who cares.
    // Whichever one it is, the +5 bonus is applied only for housing units of that type.


    protected override bool IsValidBuildingType(Building.BuildingType buildingType)
    {
        if (buildingType == Building.BuildingType.Urban) return true;
        if (buildingType == Building.BuildingType.Suburban) return true;
        if (buildingType == Building.BuildingType.Library) return true;
        if (buildingType == Building.BuildingType.Museum) return true;
        if (buildingType == Building.BuildingType.PrimarySchool) return true;
        if (buildingType == Building.BuildingType.Park) return true;
        if (buildingType == Building.BuildingType.GroceryStore) return true;

        return false;
    }

    protected override bool IsCompleteDistrict(Dictionary<Building.BuildingType, int> typesInDistrict)
    {
        bool hasUrban = typesInDistrict.ContainsKey(Building.BuildingType.Urban);
        bool hasSuburban = typesInDistrict.ContainsKey(Building.BuildingType.Suburban);
        bool hasLibrary = typesInDistrict.ContainsKey(Building.BuildingType.Library);
        bool hasMuseum = typesInDistrict.ContainsKey(Building.BuildingType.Museum);
        bool hasPrimarySchool = typesInDistrict.ContainsKey(Building.BuildingType.PrimarySchool);
        bool hasPark = typesInDistrict.ContainsKey(Building.BuildingType.Park);
        bool hasGroceryStore = typesInDistrict.ContainsKey(Building.BuildingType.GroceryStore);

        if ((hasUrban || hasSuburban) && hasLibrary && hasMuseum && hasPrimarySchool && hasPark && hasGroceryStore)
        {
            return true;
        }

        return false;
    }


    protected override float GetDistrictScore(List<Building> buildings)
    {
        float districtScore = 40;

        // 1. Is this suburban or urban?
        int urbanCount = buildings.Count(b => b.type == Building.BuildingType.Urban);
        int suburbanCount = buildings.Count(b => b.type == Building.BuildingType.Suburban);

        if (urbanCount > suburbanCount)
            districtScore += urbanCount * 5;
        else
            districtScore += suburbanCount * 5;

        return districtScore;
    }
}
